package hongcaosp.app.android.user.settings.wallet;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import hongcaosp.app.android.R;
import hongcaosp.app.android.modle.BaseResponse;
import hongcaosp.app.android.user.settings.wallet.iview.WithdrawIView;
import hongcaosp.app.android.user.settings.wallet.presenter.WithdrawPresenter;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;

/**
 * 提现
 */
public class WithdrawActivity extends BaseActivity implements WithdrawIView {

    /**
     *
     * @param activity
     * @param account
     * @param type  0-支付宝 1-微信
     * @param totalMoney
     */
    public static void launcher(Activity activity, int type, String account,double totalMoney, int requestCode) {
        Intent intent = new Intent(activity,WithdrawActivity.class);
        intent.putExtra("type",type);
        intent.putExtra("account",account);
        intent.putExtra("totalMoney",totalMoney);
        activity.startActivityForResult(intent,requestCode);
    }

    private int type;
    private String account;
    private double totalMoney;

    private TextView tv_type,tv_account,tv_next,tv_total_money;
    private EditText et_money;
    private View fl_delete;

    private WithdrawPresenter presenter;

    @Override
    protected void initData(Intent intent) {
        type = intent.getIntExtra("type",0);
        account = intent.getStringExtra("account");
        totalMoney = intent.getDoubleExtra("totalMoney",0);
        tv_total_money.setText("" + totalMoney);

        tv_type.setText(type == 0 ? "支付宝账号" : "微信账号");
        tv_account.setText(account);
    }

    @Override
    protected void initView() {
        presenter = new WithdrawPresenter(this);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findViewById(R.id.tv_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全部体现
                et_money.setText("" + totalMoney);
            }
        });
        tv_type = findViewById(R.id.tv_type);
        tv_account = findViewById(R.id.tv_account);
        tv_total_money = findViewById(R.id.tv_total_money);
        tv_next = findViewById(R.id.tv_next);
        tv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下一步
                String amountStr = et_money.getText().toString().trim();
                if (TextUtils.isEmpty(amountStr)) {
                    ToastManager.getInstance().showToast("请输入提现金额");
                    return;
                }
                double amount = Double.valueOf(amountStr);
                if (amount > totalMoney) {
                    ToastManager.getInstance().showToast("输入金额不能超过可提现总额");
                    return;
                }
                presenter.cashWithdrawal(type,amount);
            }
        });

        fl_delete = findViewById(R.id.fl_delete);
        fl_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_money.setText("");
            }
        });
        et_money = findViewById(R.id.et_money);
        et_money.addTextChangedListener(new MoneyTextWatcher(et_money, new MoneyTextWatcher.IOnTextLengthListener() {
            @Override
            public void onTextLengthChanged(int length) {
                fl_delete.setVisibility(length > 0 ? View.VISIBLE : View.GONE);
                tv_next.setEnabled(length > 0);
                if (length > 0) {
                    double amount = Double.valueOf(et_money.getText().toString().trim());
                    tv_next.setEnabled(amount > 0);
                } else {
                    tv_next.setEnabled(false);
                }
            }
        }));
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_withdraw;
    }

    @Override
    public void cashWithdrawalRst(BaseResponse response) {
        if (response != null && response.getCode() == 0) {
            ToastManager.getInstance().showToast("" + response.getMessage());

            setResult(RESULT_OK);
            finish();
            return;
        }
        ToastManager.getInstance().showToast(response == null ? "提现失败" : response.getMessage());
    }
}
